<?php
/** @abstract: Register account page
 * Author: HoanVX
 * @version: $Id: register.php 82 2015-03-12 15:33:11Z voxuanhoan@gmail.com $
 */
/*
 * Template Name: Register account page
 */


// Inluce lib
require_once (ABS_THEME_PATH .'helper/dbtransaction.php');

// Get form prorperty information
$email = "";
$password = "";
$repassword = "";
$firstName = "";
$lastName = "";
$phone = "";
$address = "";
$facebook = "";
$skype = "";
$company = "";

// Validate form
$validate = new Validation();

if (isset($_POST['submit'])) {

	$email = $_POST['email'];
	$password = $_POST['password'];
	$repassword = $_POST['repassword'];
	$firstName = $_POST['firstName'];
	$lastName = $_POST['lastName'];
	$phone = $_POST['phone'];
	$address = $_POST['address'];
	$facebook = $_POST['facebook'];
	$skype = $_POST['skype'];
	$company = $_POST['company'];

	// Check Email
	if(empty($email))
		$validate->setError(array('itemName'=>'email', 'message'=>Messages::ERROR_COMMON_EMAIL_REQUIRED));
	if (strlen($email) > 100)
		$validate->setError(array('itemName'=>'email', 'message'=>Messages::ERROR_COMMON_EMAIL_MAXLENGTH));
	if(!is_email($email))
		$validate->setError(array('itemName'=>'email', 'message'=>Messages::ERROR_COMMON_EMAIL_INVALID));

	// Check password
	if(empty($password))
		$validate->setError(array('itemName'=>'password', 'message'=>Messages::ERROR_COMMON_PASSWORD_REQUIRED));
	if(strlen($password) < 6)
		$validate->setError(array('itemName'=>'password', 'message'=>Messages::ERROR_COMMON_PASSWORD_MINLENGTH));
	if(strlen($password) > 20)
		$validate->setError(array('itemName'=>'password', 'message'=>Messages::ERROR_COMMON_PASSWORD_MAXLENGTH));

	// Check repassword
	if(strcasecmp($password, $repassword) != 0) {
		$validate->setError(array('itemName'=>'repassword', 'message'=>Messages::ERROR_REGISTER_PASSWORD_MAPPING));
	}

	// Check first name
	if(empty($firstName))
		$validate->setError(array('itemName'=>'firstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_REQUIRED));
	if(strlen($firstName) < 2) {
		$validate->setError(array('itemName'=>'firstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_MINLENGTH));
	}
	if(strlen($firstName) > 20) {
		$validate->setError(array('itemName'=>'firstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_MAXLENGTH));
	}

	// Check last name
	if (empty($lastName)) {
		$validate->setError(array('itemName'=>'lastName', 'message'=>Messages::ERROR_REGISTER_LASTNAME_REQUIRED));
	}
	if(strlen($lastName) > 10 ) {
		$validate->setError(array('itemName'=>'lastName', 'message'=>Messages::ERROR_REGISTER_LASTNAME_MAXLENGTH));
	}

	// Check phone
	if(!empty($phone)) {
		if(!is_numeric($phone))
			$validate->setError(array('itemName'=>'phone', 'message'=>Messages::ERROR_REGISTER_PHONE_NUMBER));
		if(strlen($phone) < 10)
			$validate->setError(array('itemName'=>'phone', 'message'=>Messages::ERROR_REGISTER_PHONE_MINLENGTH));
		if(strlen($phone) > 15)
			$validate->setError(array('itemName'=>'phone', 'message'=>Messages::ERROR_REGISTER_PHONE_MAXLENGTH));
	}

	// Check dia chi
	if(!empty($address) && strlen($address) > 150)
		$validate->setError(array('itemName'=>'address', 'message'=>Messages::ERROR_REGISTER_ADDRESS_MAXLENGTH));

	// Check facebook
	if(!empty($facebook) && strlen($facebook) > 50)
		$validate->setError(array('itemName'=>'facebook', 'message'=>Messages::ERROR_REGISTER_FACEBOOK_MAXLENGTH));

	// Check facebook
	if(!empty($skype) && strlen($skype) > 50)
		$validate->setError(array('itemName'=>'skype', 'message'=>Messages::ERROR_REGISTER_SKYPE_MAXLENGTH));

	// Check company
	if(!empty($company) && strlen($company) > 200)
		$validate->setError(array('itemName'=>'company', 'message'=>Messages::ERROR_REGISTER_COMPANY_MAXLENGTH));

	if(!$validate->hasError()) {
		$transaction = new DBTransaction();
		try {
			$transaction->beginTransaction();

			$userdata = array(
					'user_login' 	=> $email, //use email to make username for login
					'user_pass' 	=> $password,
					'user_email' 	=> $email,
					'first_name' 	=> $firstName,
					'last_name' 	=> $lastName,
					'display_name'	=> $firstName . " " . $lastName,
					'role' 			=> 'subscriber' //lowest role
			);

			$user_id = wp_insert_user( $userdata );

			if ( ! $user_id || is_wp_error( $user_id ) ) {

				foreach ($user_id->get_error_codes() as $key) {
					if(strcmp($key, "existing_user_login") == 0 || (strcmp($key, "existing_user_email") == 0)) {
						$validate->setError(array('itemName'=>'email', 'message'=>Messages::ERROR_COMMON_EMAIL_EXISTED));
					}
				}
				goto displayHTML;
			}
			// Convert to blank if its value is blank
			$phone = empty($phone) ? "" : $phone;
			$address = empty($address) ? "" : $address;
			$facebook = empty($facebook) ? "" : $facebook;
			$skype = empty($skype) ? "" : $skype;
			$company = empty($company) ? "" : $company;

			// Add meta data : prefix: ct:  cho troi
			// phone
			add_user_meta($user_id, CT_PHONE, $phone);
			// address
			add_user_meta($user_id, CT_ADDRESS, $address);
			// facebook
			add_user_meta($user_id, CT_FACEBOOK, $facebook);
			//skype
			add_user_meta($user_id, CT_SKYPE, $skype);
			// company
			add_user_meta($user_id, CT_COMPANY, $company);


			//Auto login after regist account successful
			$creds = array('user_login'=> $email
						, 'user_password' =>$password
						, 'remember' =>false
			);
			global $isUserLogin;
			$isUserLogin = wp_signon($creds, false);

			// Commit transaction
			$transaction->commit();

			// Rediect to register-account-complete
			// register-complete.php
			wp_redirect(get_the_permalink(168));

		} catch (Exception $e) {
			$transaction->rollBack();
		}
	}
}
displayHTML:
/** header-section-ends */
// add file to header
add_style("css/page/register.css");
add_script("js/jquery.validate.js");
add_script("js/jquery.flexisel.js");
add_script('js/page/register.js');
get_header();
?>
	<!-- header-section-ends -->
	<!-- content-section-starts -->
<div class="content">
	<div class="container register">
		<div class="col-md-7 ">
      		<h2 class="text-success">Đăng ký tài khoản</h2>
      		<?php
      		if ($validate->hasMessage()) {
      			echo '<div class="panel panel-info">';
      			echo 	'<div class="panel-heading">';
      			echo $validate->getMessage('success');
      			echo 	'</div>';
      			echo '</div>';
      		}

      		?>
      		<h3 class="wow fadeInRight" data-wow-delay="0.4s"><strong><i>Lưu ý &nbsp;</i></strong>Các mục có dấu<span class="required">&nbsp;*&nbsp;</span>là bắt buộc nhập</h3>
	      	<form class="form-horizontal" role="form" method="POST" id="registForm">
	      		<h3 class="wow fadeInRight" data-wow-delay="0.4s">Thông tin tài khoản</h3>
	        	<div class="form-group wow fadeInLeft" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="email">Email <span class="required">(*) </span>:</label>
	          		<div class="col-sm-8">
            			<input type="email" class="form-control" id="email" placeholder="Nhập email" name="email" value="<?php echo esc_attr($email);?>" maxlength="100">
          				<?php echo $validate->getError('email'); ?>
          			</div>
	        	</div>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="password">Mật khẩu <span class="required">(*) </span>:</label>
	          		<div class="col-sm-8">
	            		<input type="password" class="form-control" id="password" placeholder="Nhập mật khẩu" name="password" value="<?php echo esc_attr($password);?>" maxlength="20">
	          			<?php echo $validate->getError('password');?>
	          		</div>
	        	</div>
        		<div class="form-group wow fadeInLeft" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="repassword">Nhập lại mật khẩu <span class="required">(*) </span>:</label>
	          		<div class="col-sm-8">
	            		<input type="password" class="form-control" id="repassword" name="repassword" placeholder="Nhập lại mật khẩu" value="<?php echo esc_attr($repassword);?>" maxlength="20">
	          			<?php echo $validate->getError('repassword');?>
	          		</div>
	        	</div>
	        	<h3 class="wow fadeInLeft"  data-wow-delay="0.4s">Thông tin cá nhân</h3>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="firstName">Họ đệm <span class="required">(*) </span>:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="firstName" name="firstName" placeholder="Nhập họ đệm" value="<?php echo esc_attr($firstName);?>" maxlength="20">
	            		<?php echo $validate->getError('firstName');?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInLeft" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="lastName">Tên <span class="required">(*) </span>:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Nhập tên" value="<?php echo esc_attr($lastName);?>" maxlength="10">
	          			<?php echo $validate->getError('lastName');?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="phone">Số điện thoại:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="phone" name="phone" placeholder="Nhập số điện thoại" value="<?php echo esc_attr($phone);?>" maxlength="15">
            			<?php echo $validate->getError('phone');?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="address">Địa chỉ của bạn:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="address" name="address" placeholder="Nhập vào địa chỉ của bạn" value="<?php echo esc_attr($address);?>" maxlength="150">
            			<?php echo $validate->getError("address");?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="facebook">Facebook:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="facebook" name="facebook" placeholder="Nhập vào tài khoản facebook của bạn" value="<?php echo esc_attr($facebook);?>" maxlength="50">
            			<?php echo $validate->getError("facebook");?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInRight" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="skype">Skype:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="skype" name="skype" placeholder="Nhập vào tài khoản skype của bạn" value="<?php echo esc_attr($skype);?>" maxlength="50">
            			<?php echo $validate->getError("skype");?>
	          		</div>
	        	</div>
	        	<div class="form-group wow fadeInLeft" data-wow-delay="0.4s">
	          		<label class="control-label col-sm-4" for="company">Công ty:</label>
	          		<div class="col-sm-8">
	            		<input type="text" class="form-control" id="company" name="company" placeholder="Nhập tên công ty của bạn" value="<?php echo esc_attr($company);?>" maxlength="200">
	            		<?php echo $validate->getError('company');?>
	          		</div>
	        	</div>
		        <div class="form-group wow fadeInRight" data-wow-delay="0.4s">
		          	<div class="col-sm-offset-2 col-sm-10 register-but">
		            	<input type="submit" class="btn btn-default" name="submit" value="Đăng ký"/>
		          	</div>
		        </div>
		      </form>
		</div>
		<!-- End thong tin dang ky -->

		<!-- Dieu khoan su dung -->
		<div class="col-md-5" id="note">
			<h2 class="text-success">Điều khoản sử dụng</h2>
			<?php
			if(have_posts()) :
				while (have_posts()) : the_post();
					the_content();
				endwhile;
			endif;
			?>
		</div>
		<!-- END Dieu khoan su dung -->
    </div>
    <?php
    // TODO: HOANVX WILL ADDED THIS SECTION LATER
    //include_once ("commons/ads-section.php");
    ?>

	<input type="hidden" value="0" name="createUserStatus">

	<!-- Contact's information -->
	<?php get_footer("contact");?>
	<!-- END Contact's information -->
</div>
	<!-- content-section-ends -->
	<!-- footer-section-starts -->
	<?php get_footer();?>
